<?php

class WareHelper{

	/**
	  生成模糊搜索的字段sql
	  */
	static function makupFuzzyKey($val){
		$part	= 'LIKE "%'.addslashes($val).'%"';
		$where	= array();
		foreach(array('name','sno','brand','specific','remark') as $k){
			$where[] = "`$k` $part";
		}
		return '('.join(' OR ', $where).')';
	}

	static function getList($where, $limited=true, $order='sno ASC'){
		$c = self::count($where);
		$limit = null;
		if($limited)
		$limit = Lib::isprint() ? null : PagerHelper::split2limit($c);
		return Db::get('Ware')->select()->where($where)
					->limit($limit)
					->order($order)->getAll();
	}
        
        static function getListWithSuppliers($where, $limited=true, $order='sno ASC'){
		$c = self::count($where);
		$limit = null;
		if($limited)
		$limit = Lib::isprint() ? null : PagerHelper::split2limit($c);
		$tmp =  Db::get('Ware')->select()->where($where)
					->limit($limit)
					->order($order)->getAll();
                if($tmp)
                {
                    foreach($tmp as $k=>$v)
                    {
                        $tmp[$k]['items']=Db::get('_supplier_quote')->select('supplier_name,quote')->where(array('ware_id'=>$v['id']))->order('quote ASC')->getAll();
                    }
                }
                
                return $tmp;
                
	}

	static function count($where){
		return Db::get('Ware')->count()->where($where)->getCol();
	}

	/**
	 * 取得需要进行补库采购的材料列表
	 */
	static function getRefill(){
		return Db::get('Ware')->select('*, cordon-amount as refill')->where(array('amount','<','cordon'))->getAll();
	}

}

